package com.demo.common.config;

import org.apache.log4j.Logger;
import org.beetl.core.GroupTemplate;
import org.eclipse.jetty.server.handler.ContextHandler;

import com.alibaba.druid.wall.WallFilter;
import com.demo.beetl.func.SubString;
import com.demo.beetl.render.MyBeetlRenderFactory;
import com.demo.common.model._MappingKit;
import com.demo.controller.AdminController;
import com.demo.controller.LoginController;
import com.demo.controller.UeditorController;
import com.demo.controller.UploadController;
import com.demo.controller.admin.ArticleAdminController;
import com.demo.controller.admin.AuthUserController;
import com.demo.controller.admin.BaseInfoAdminController;
import com.demo.controller.admin.CategoryAdminController;
import com.demo.controller.admin.LogAdminController;
import com.demo.controller.admin.NavAdminController;
import com.demo.controller.admin.TagAdminController;
import com.demo.controller.admin.UploadAdminContrller;
import com.demo.controller.admin.UploadConfigController;
import com.demo.controller.front.ArticleController;
import com.demo.controller.front.BaseInfoController;
import com.demo.controller.front.CategoryController;
import com.demo.controller.front.TagController;
import com.demo.handler.HtmlHandler;
import com.demo.index.IndexController;
import com.demo.interceptor.CommonInterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.render.ViewType;

/**
 * API引导式配置
 */
public class DemoConfig extends JFinalConfig {
	private static Logger logger = Logger.getLogger(DemoConfig.class);
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		logger.info("CONSTANTS STARTING");
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setViewType(ViewType.FREE_MARKER);
		me.setMainRenderFactory(new MyBeetlRenderFactory());
		GroupTemplate groupTemplate = MyBeetlRenderFactory.groupTemplate;
		groupTemplate.registerFunction("subString", new SubString());
		logger.info("CONSTANTS  END");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		logger.info("Routes config  starting");
		me.add("/", IndexController.class, "/index");	// 第三个参数为该Controller的视图存放路径
		//me.add("/blog/login", LoginController.class);  // 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
		me.add("/ueditor", UeditorController.class); 
		me.add("/upload", UploadController.class);   //上传
		//后台路由
		adminRoute(me);
		//前台路由
		frontRoute(me);
	}
	//后台路由
	public void adminRoute(Routes me){
		me.add("/admin/login", LoginController.class);  
		me.add("/admin", AdminController.class);  
		me.add("/admin/user", AuthUserController.class);  // 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
		me.add("/admin/article", ArticleAdminController.class);  // 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
		me.add("/admin/tag", TagAdminController.class); 
		me.add("/admin/category", CategoryAdminController.class); 
		me.add("/admin/uploadConfig", UploadConfigController.class); 
		me.add("/admin/baseInfo", BaseInfoAdminController.class); 
		me.add("/admin/upload", UploadAdminContrller.class); 
		me.add("/admin/nav", NavAdminController.class); 
		me.add("/admin/log", LogAdminController.class); 
		
	}
	//前台路由
	public void frontRoute(Routes me){
		me.add("/page/article", ArticleController.class); 
		me.add("/tag", TagController.class);
		me.add("/category", CategoryController.class);
		
	}
	
/*	public static C3p0Plugin createC3p0Plugin() {
		logger.info("C3p0Plugin config  Starting");
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}*/
	
	public static DruidPlugin createDruidPlugin(){
		logger.info("DruidPlugin config  Starting");
		return  new DruidPlugin(PropKit.get("jdbcUrl"),PropKit.get("user"), PropKit.get("password"));
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		logger.info("Plugins config  Starting");
		DruidPlugin dp = createDruidPlugin();
	    WallFilter wf = new WallFilter();
	    wf.setDbType("mysql");
	    dp.addFilter(wf);
	    me.add(dp);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		me.add(arp);
		arp.setDevMode(true);
        arp.setShowSql(true);
		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
		logger.info("Plugins config  end");
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		logger.info("Interceptors config  Starting");
		me.add(new SessionInViewInterceptor());
		me.add(new CommonInterceptor());
		logger.info("Interceptors config  end");
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		logger.info("Handlers config  starting");
		me.add(new ContextPathHandler("ctx"));
		me.add(new HtmlHandler());
		DruidStatViewHandler dsvh = new DruidStatViewHandler("/druid");
		me.add(dsvh);
		logger.info("Handlers config  end");
		
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}
}
